<?
class Shop_Game_Form_Buy extends Zend_Form
{
  function __construct($opts = null,$param = array())
  {
    parent::__construct($opts);
    $this->setName('login');

    $session = new Zend_Session_Namespace('shop');
    $username = $this->createElement('text', 'user', array(
      'label' => 'ไอดี',
      'value'=>$session->game_user_name,
      'errorMessages'=>array('กรุณาระบุ ไอดี')
    ));
    $username->setRequired(true);

    $password = $this->createElement('password', 'pass', array('label' => 'รหัสผ่าน','errorMessages'=>array('กรุณาระบุ รหัสผ่าน')));
    $password->addValidator('StringLength', false, array(6))
             ->setRequired(true);

    $email = $this->createElement('text', 'email', array('label' => 'อีเมล์','errorMessages'=>array('กรุณาระบุ อีเมล์')));
    $email->setRequired(true);
    //$username->setRequired(true);

//    $type_options = array("ppl"=>"Paypal");
//    //$type_options = array("ccd"=>"Credit card","psb"=>"Paysbuy","ppl"=>"Paypal");
//    $type = $this->createElement("select","pay_type",array('label' => 'วิธีการจ่ายเงิน'));
//      $type->setMultiOptions($type_options)
//                ->setRequired(true)
//                ->addFilter('stringTrim')
//                ->addValidator('inArray', false, array(array_keys($type_options)));

    $amount = $this->createElement("text","amount",array('label' => 'จำนวน','value'=>1));
    $amount->addValidator('alnum', true);
    $product_id = $this->createElement("hidden","product_id",array('value'=>intval(@$param['product_id'])));

    $this->addElement($username);
    $this->addElement($password);
    $this->addElement($email);
    //$this->addElement($type);
    $this->addElement($amount);
    $this->addElement($product_id);
    $this->addElement('submit', 'submit', array('label' => 'ขั้นต่อไป'));

    $this->addDecorator('Description', array('placement' => 'append'));
  }

  public function isValid($data)
  {
    $r = parent::isValid($data);
    if (!$r) return $r;
    if ($data['amount'] < 1)
    {
      $this->amount->addError('การซื้อสิ้นค้าขั้นต่ำคือ 1 ชิ้น');
      $r = false;
    }
    if(!preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$/',$data['email']))
    {
    	$this->amount->addError('รูปแบบ email ผิดพลาด');
    	$r = false;
    }
    return $r;
  }
}
